﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;

namespace SpeedTest
{
	public enum AdjustmentType
	{
		Increment,
		Decrement
	}

	public class NodePath
	{
		private string _path = String.Empty;

		public NodePath(string path)
		{
			_path = path;
		}

		public NodePath()
		{
		}

		public string Path
		{
			get
			{
				return _path;
			}
			set
			{
				_path = value;
			}
		}

		public string Order
		{
			get
			{
				if (_path.Length >= 3)
				{
					return _path.Substring(_path.Length - 3, 3);
				}
				else { return String.Empty; }
			}
		}

		public string FirstChild
		{
			get
			{
				return _path + ".001"; //first child should always be this.
			}
		}

		public string Parent
		{
			get
			{
				if (_path.Length >= 7)
				{
					return _path.Substring(0, _path.Length - 4);	//the last three are the order, plus one for the dot
				}
				else { return String.Empty; }	//the next smallest is a 3 digit path i.e. 001, so the parent is empty
			}
		}

		public int Level
		{
			get
			{
				if (!String.IsNullOrEmpty(_path))
				{
					string[] pathParts = _path.Split('.');

					return pathParts.Length;
				}
				else { return 0; }
			}
		}

		public string SwapPathSection(string oldSection, string newSection)
		{
			if (_path.StartsWith(oldSection))
			{
				return newSection + _path.Substring(oldSection.Length, _path.Length - oldSection.Length);
                    //_path.(oldSection, newSection);
			}
			else { return _path; }
		}

		public string Increment()
		{
			return Increment(this.Level);
		}

		public string Increment(int level)
		{
			return AdjustPath(_path, level, AdjustmentType.Increment);
		}

		public string Decrement()
		{
			return Decrement(this.Level);
		}

		public string Decrement(int level)
		{
			return AdjustPath(_path, level, AdjustmentType.Decrement);
		}

		public string AdjustPath(string path, int level, AdjustmentType adjType)
		{
			string[] pathParts = path.Split('.');
			if (pathParts.Length >= level)
			{
				int pathLevel = 0;
				Int32.TryParse(pathParts[level - 1], out pathLevel);

				if (adjType == AdjustmentType.Increment) { pathLevel++; }
				if (adjType == AdjustmentType.Decrement) { pathLevel--; }

				string combinedPath = String.Empty;

				for (int i = 0; i < pathParts.Length; i++)
				{
					if (i == level - 1)
					{
						combinedPath += pathLevel.ToString("000");
					}
					else
					{
						combinedPath += pathParts[i];
					}

					if (i != pathParts.Length - 1) //if it's not the last one, add a period
					{
						combinedPath += ".";
					}
				}

				return combinedPath;
			}
			else
			{
				return path;    //keep original
			}
		}
	}

	public static class PathUtil
	{
        public static void ChangePathParents(ref DataTable table, string newParentPath, string oldParentPath)
        {
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
					NodePath path = new NodePath(table.Rows[i]["Path"].ToString());
					path.Path = path.SwapPathSection(oldParentPath, newParentPath);
					table.Rows[i]["Path"] = path.Path;
                }
            }
        }

        public static void IncrementPaths(ref DataTable table, int level)
        {
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
					NodePath path = new NodePath(table.Rows[i]["Path"].ToString());
					path.Path = path.Increment(level);
					table.Rows[i]["Path"] = path.Path;
                }
            }
        }

        public static void DecrementPaths(ref DataTable table, int level)
        {
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
					NodePath path = new NodePath(table.Rows[i]["Path"].ToString());
					path.Path = path.Decrement(level);
					table.Rows[i]["Path"] = path.Path;
                }
            }
        }
	}

    public class NamedParameter
    {
        private string _name = String.Empty;
        private string _value = String.Empty;

		public NamedParameter()
		{

		}

		public NamedParameter(string name, string value)
		{
			_name = name;
			_value = value;
		}

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public string Value
        {
            get
            {
                return _value;
            }
            set
            {
                _value = value;
            }
        }
    }
}
